package com.concurrent.data.thread.controller;

import com.concurrent.data.thread.common.Result;
import com.concurrent.data.thread.entity.Consumer;
import com.concurrent.data.thread.service.ConsumerService;
import com.concurrent.data.thread.service.QueueProvider;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 哲思小辉
 * @version 1.0.0
 * @ClassName ConsumerController.java
 * @Description 顾客控制类
 * @Param
 * @createTime 2020年03月25日 20:35:00
 */
@Component
@RestController
@RequestMapping("consumer")
@Api(value = "顾客控制类", tags = "顾客控制类")
@Slf4j
public class ConsumerController {
    /**
     * 服务对象
     */
    @Autowired
    private ConsumerService consumerService;

    @Autowired
    private QueueProvider queueProvider;

    @PostMapping("addConsumer")
    @ApiOperation(value = "新增顾客", notes = "新增顾客")
    public Result<Consumer> addConsumer(@RequestBody Consumer consumer) {
        log.info("新增数据：{}", consumer.toString());
        Consumer insert = this.consumerService.insert(consumer);
        return Result.success(consumer);
    }

    @PostMapping("updateConsumer")
    @ApiOperation(value = "修改顾客", notes = "修改顾客")
    @CachePut(cacheNames = "consumer", condition = "#consumer.id > 100", key = "#consumer.id")
    public Result<Consumer> updateConsumer(@RequestBody Consumer consumer) {
        log.info("修改数据：{}", consumer.getId());
        Consumer insert = this.consumerService.update(consumer);
        return Result.success(consumer);
    }

    @GetMapping("getConsumer")
    @ApiOperation(value = "获取顾客", notes = "获取顾客")
    @Cacheable(cacheNames = "consumer", condition = "#consumerId > 100", key = "#consumerId", sync = true)
    public Result<Consumer> getConsumer(Integer consumerId) {
        log.info("查询数据：{}", consumerId);
        Consumer consumer = consumerService.queryById(consumerId);
        return Result.success(consumer);
    }

    @GetMapping("deleteConsumer")
    @ApiOperation(value = "删除顾客", notes = "删除顾客")
    @CacheEvict(cacheNames = "consumer", condition = "#consumerId > 100", key = "#consumerId")
    public Result<Boolean> deleteConsumer(Integer consumerId) {
        log.info("删除数据：{}", consumerId);
        boolean delete = consumerService.deleteById(consumerId);
        return Result.success(delete);
    }


    @PostMapping(value = "/setSession")
    public Result setSession(HttpServletRequest request) {
        request.getSession().setAttribute("message", request.getRequestURL());
        return Result.success();
    }

    @PostMapping(value = "/getSession")
    public Result<Map<String, Object>> getSession(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        map.put("message", request.getSession().getId());
        map.put("message", request.getSession().getAttribute("message"));
        return Result.success(map);
    }

    @PostMapping(value = "/testSend")
    public Result testSend(String content) {
        queueProvider.send(content);
        log.info("发送信息成功：msg = {}", content);
        return Result.success();
    }


}